package com.lee.demo.ui.activity;

import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.widget.GridLayout;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lee.demo.R;
import com.lee.demo.base.BaseActivity;
import com.lee.demo.base.SwipeBackActivity;
import com.lee.demo.model.GridListBean;
import com.lee.demo.util.GridLayoutUtil;

import java.util.ArrayList;
import java.util.List;

import cn.lee.cplibrary.util.ScreenUtil;

/**
 * 网格布局、横向RecyclerView
 * create by ChrisLee on 2021/9/15
 */
public class GridLayoutActivity extends SwipeBackActivity {

    private RecyclerView recyclerView;
    private GridLayout gridL_hot;
    /**
     * 动态设置GridLayout布局
     */
    private GridLayout gridL_dynamic;
    public static final String url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp05%2F19100215045a245-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1635150447&t=ea6eb6360eae6fde0113b0f12789c2fe"; //图片模拟


    @Override
    protected BaseActivity getSelfActivity() {
        return this;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_grid_layout;
    }

    @Override
    public String getPagerTitle() {
        return null;
    }

    @Override
    public String getPagerRight() {
        return "";
    }

    @Override
    public void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        gridL_hot = (GridLayout) findViewById(R.id.gridL_hot);
        gridL_dynamic = (GridLayout) findViewById(R.id.gridL_dynamic);

        initRecyclerView();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initData() {

    }

    private void initRecyclerView() {
        List<String> totalList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            totalList.add("");
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(getSelfActivity(), LinearLayoutManager.HORIZONTAL, false));
        recyclerView.setAdapter(new PhotoAddAdapter(this, totalList));
    }

    public class PhotoAddAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
        private List<String> totalList;
        private Activity activity;


        public PhotoAddAdapter(Activity activity, List<String> totalList) {
            super(R.layout.item_img, totalList);
            this.totalList = totalList;
            this.activity = activity;
            setOnItemClickListener(new OnItemClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    gridL_dynamic.removeAllViews();
                    GridLayoutUtil.createGridLayout(getSelfActivity(), getGridList(position + 1), gridL_dynamic, ScreenUtil.dp2px(getSelfActivity(), 145), ScreenUtil.dp2px(getSelfActivity(), 9), ScreenUtil.dp2px(getSelfActivity(), 9));

                }
            });
        }

        @Override
        protected void convert(final BaseViewHolder helper, final String bean) {

        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public List<GridListBean> getGridList(int size) {
        List<GridListBean> listBeans = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            listBeans.add(new GridListBean(String.valueOf(i), "测试" + String.valueOf(i), url));
        }
        return listBeans;
    }
}
